/**
 * Created by Adam on 2017/5/16.
 */
const user = (state = {}, action) => {
    switch (action.type) {
        case 'ADD_USER':
            return {
                id: action.id,
                name: action.name,
                age: action.age,
                gender: 'Female'
            }
        case 'TOGGLE_GENDER':
            if (state.id !== action.id) {
                return state
            }
            let newGender = state.gender === 'Female' ? 'Male' : 'Female'
            return {
                ...state,
                gender: newGender
            }
        default:
            return state
    }
}

const users = (state = [], action) => {
    switch (action.type) {
        case 'ADD_USER':
            return [
                ...state,
                user(undefined, action)
            ]
        case 'TOGGLE_GENDER':
            return state.map(t =>
                user(t, action)
            )
        default:
            return state
    }
}

export default users